﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
//using System.Data.EntityClient;
using eGrocery.BaseDAL;
using eGrocery.BaseDAL.Model;

namespace eGrocery.LocationDAL
{
    public class CityDA : eGrocery.BaseDAL.BaseDA<City>
    {
        public override BaseDAL.Model.City SaveObject(string userName, BaseDAL.Model.City tObject)
        {
            try
            {
                if (tObject != null)
                {

                    //tObject.Save(); 
                    if (tObject.ID == 0)
                        Global.Context.Cities.Add(tObject);
                    else
                        Global.Context.Entry(tObject).State = EntityState.Modified;
                    Global.Context.SaveChanges();
                }
            }
            catch (Exception)
            {
                tObject = null;
                throw;
            }
            return tObject;
        }

        public override bool DeleteObject(string userName, BaseDAL.Model.City tObject)
        {
            bool retValue;
            try
            {
                retValue = true;
                tObject.Delete();
            }
            catch (Exception)
            {
                retValue = false;
                throw;
            }
            return retValue;
        }

        public override List<BaseDAL.Model.City> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            return Global.Context.Cities.Where(m => m.ISLIVE == true).ToList();
        }

        public override List<BaseDAL.Model.City> SearchObject(string userName, string SearchText)
        {
            throw new NotImplementedException();
        }
        //public override BaseDAL.Model.City SaveObject(string userName, BaseDAL.Model.City tObject)
        //{
        //    try
        //    {
        //        GetNewDBHelper();
        //        tObject.Save();               
        //    }
        //    catch (Exception)
        //    {

        ////throw;
        //    }
        //    return tObject;



        //    // throw new NotImplementedException();
        //}

        //public override bool DeleteObject(string userName, BaseDAL.Model.City tObject)
        //{
        //    try
        //    {
        //        tObject.Delete();
        //        //dbHelper.AddParameter("@LocationID", tObject.ID);
        //        //dbHelper.ExecuteNonQuery("USP_INS_DELETE_CITY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
        //        //// throw new NotImplementedException();
        //        //return tObject.IsLive;
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //    //throw new NotImplementedException();
        //}

        //public override List<BaseDAL.Model.City> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        //{
        //    return Global.Context.Cities.ToList();
        //}

        //public override List<BaseDAL.Model.City> SearchObject(string userName, string SearchText)
        //{

        //    try
        //    {
        //        return Global.Context.Cities.Where(k =>
        //            k.DISPLAY_NAME.Contains(SearchText) || k.FULL_NAME.Contains(SearchText)).Take(20).ToList();
        //    }
        //    catch (Exception ex)
        //    {
        //        LogException(userName, ex, null);
        //        return null;
        //        //throw new Exception("Error while searching City");
        //    }
        //    // throw new NotImplementedException();
        //}

        //public static LocationEntity.City GetCityObject(IDataReader reader, bool IsFull)
        //{
        //    int i = -1;
        //    LocationEntity.City obj = new LocationEntity.City();
        //    if (!reader.IsDBNull(++i))
        //        obj.ID = reader.GetInt32(i);
        //    if (!reader.IsDBNull(++i))
        //        obj.FullName = reader.GetString(i);
        //    if (!reader.IsDBNull(++i))
        //        obj.DisplayName = reader.GetString(i);

        //    if (IsFull)
        //    {

        //        if (!reader.IsDBNull(++i))
        //            obj.IsLive = reader.GetBoolean(i);
        //    }
        //    return obj;
        //}



    }
}
